function [matf, err] = getsetini(mode, matfile)
% read an initialization file

matf=[];
err=0;
if(strcmp(computer,'PCWIN'))
    filename = 'c:\mat_datac\datacmat.ini';
    drive = 'd:'; % internal defaults
    %display('mode is pc')
else
    filename='datacmat.ini';
    drive='';
    %display('mode is unix')
end

extension = '*.pbm'; % internal defaults - override with the ini file
path = '';
switch (mode)
    case 'get'
        fid = fopen(filename, 'r');
        if(fid <= 0)
            QueMessage(sprintf('Ini file %s not found', filename));
            err=1;
            return;
        end
        while(~feof(fid))
            line = fgetl(fid);
            switch(argument(line))
                case 'drive' % drive includes path..
                    drive = value(line);
                    if(~isempty(drive)) setdriveext('drive', drive); end
                case 'path'
                    path = value(line);
                    if(~isempty(path)) setdriveext('path', path); end
                case 'ext'
                    extension = value(line);
                    if(~isempty(extension)) setdriveext('ext', extension); end
                case 'expt'
                    matf = char(value(line));
                    matf = matf(find(matf > 20)); % clean up the filename - only valid chars (sometimes we get odd chars at end)
                    if(~isempty(matf))
                        datac('loadmatctl', matf); % attempt to load it...
                    else
                        QueMessage('No Control file');
                    end
                case 'vgain'
                    vgain = value(line);
                    if(~isempty(vgain)) set_window_arg('Vgain', vgain);
                    end
                case 'igain'
                    igain = value(line);
                    if(~isempty(igain)) set_window_arg('Igain', igain);
                    end
                case 'units'
                    units = value(line);
                    if(~isempty(units)) set_units(str2num(units));
                    end
                case 'mode'
                    mode = value(line);
                    if(~isempty(mode)) setdatamode(str2num(mode));
                    end

                otherwise
            end
        end
        fclose(fid);
        return;

    case 'set'
        fid = fopen(filename, 'wt'); % write in text mode...
        if(fid <= 0)
            err=1;
            return;
        end
        [drive, path, ext] = getdriveext;
        fprintf(fid, 'drive = %2s\n', drive); % drive includes path...
        fprintf(fid, 'path = %s\n', path);
        fprintf(fid, 'ext = %4s\n', ext);
        if(isempty(matfile)) matfile = '<none>';
        end
        fprintf(fid, 'expt = %s\n', matfile); % write the name of the current matfile
        fprintf(fid, 'vgain = %-8.3f\n', get_window_arg('Vgain', 10));
        fprintf(fid, 'igain = %-8.3f\n', get_window_arg('Igain', 1));
        fprintf(fid, 'units = %d\n', get_units);
        fprintf(fid, 'mode = %d\n', getdatamode);
        fclose(fid);

    otherwise
end
return

function [arg] = argument(line)
len=length(line);
arg = deblank(strtok(line));
return;

function [value] = value(line)
len = length(line);
a = findstr(line, '= ');
value = deblank(line(a+2:len));
return;


